#!/bin/bash
#script to update files in CARBLogger
#10/31/2012 MQ

#set variables
U_LOG="/home/aqdms/CARBLogger_upgrade_log"
date_stamp=$(date)
carblog_path="/home/aqdms/CARBLog"
#carblog_path="/home/aqdms/testme"
home_path="/home/aqdms"
restart_flag=0 #flag to indicate file change, CARBLogger requires restart of drivers
#restart_all_flag=0 #flag to indicate interface.sh change, the frontend needs complete restart
update_path="/home/aqdms/update" #carblog_path working folder for updates
copy_all_flag=0

#helper functions

#create directory function
create_directory () {
	mkdir $1
	chown aqdms:aqdms $1
	chmod 755 $1
}

############   file creation function #############################################
#
#  Copies files, writes log messages, and changes permissions and owner
#  Takes 5 arguments
#  $1 = type of log message (copy, missing, change)
#  $2 = source directory
#  $3 = target directory
#  $4 = filename
#  $5 = recursive copy flag (r if recursive, nothing if single file/non-recursive directory copy)
#
###################################################################################
copy_file () {
	file_name=$4
	source=$2
	target=$3
	copy_type=$5
	#message choice
	if [ $1 = "copy" ]; then
		echo "$date_stamp :: copying $source to $target" >> $U_LOG
	elif [ $1 = "missing" ]; then
		echo "$date_stamp :: Missing $file_name - copying $source to $target" >> $U_LOG 
	elif [ $1 = "change" ]; then
		echo "$date_stamp :: Difference in $file_name - copying $source to $target" >> $U_LOG
	else
		echo "Unknown flag in copy_file call" >> U_LOG
	fi
	if [ $copy_type = "r" ]; then
		cp -r $source $target
		chown -R aqdms:aqdms $target
		chmod -R 755 $target
		rm -rf $target/.svn
	else
		cp -f $source $target
		chown aqdms:aqdms $target
		chmod 755 $target
	fi

}


##Check for sv folder "update". 
##If present run svn update against folder. If missing, create working folder using readonly checkout
echo "**************** $date_stamp : Begin upgrade *****************" >> $U_LOG
if [ -d "$update_path" ]; then
	echo "$date_stamp :: Updating svn" >> $U_LOG
	svn update $update_path &>/dev/null
	wait
else
	echo "$date_stamp :: checking out SVN" >> $U_LOG
	svn checkout http://carblogger.googlecode.com/svn/trunk/ $update_path &>/dev/null
	wait
fi
## Check for CARBLog folder, if not there create it #########################
if [ ! -e $carblog_path ]; then
	echo "$date_stamp :: No CARBLog folder - creating CARBLog structure" >> $U_LOG
	#make CARBLog folder
	create_directory $carblog_path
	wait
	#make Data folder
	copy_file missing $update_path/Data $carblog_path/Data Data r
	wait
	#make Config Folder
	copy_file missing $update_path/Config $carblog_path/Config Config r
	wait
	#make resources folder
	copy_file missing $update_path/resources $carblog_path/resources resources r
	wait
	#make sys_resources folder
	copy_file missing $update_path/sys_resources $carblog_path/sys_resources sys_resources r
	wait
	#make Documentation folder
	copy_file missing $update_path/Documentation $carblog_path/Documentation Documentation r
	wait
	#make template folder
	copy_file missing $update_path/templates $carblog_path/templates templates r
	wait
	
	copy_all_flag=1
fi

###copy Data files and Config Files ###########
#get files in Data

#if [ "$copy_all_flag" -eq 1 ]; then
#	current_folder="Data"
#	files=$(ls -a $update_path/$current_folder 2>/dev/null)
#	for i in files; do
#		if [ -f "$update_path/$current_folder/$i" ] && [ "$i" != ".svn" ]; then
#			copy_file missing $update_path/$current_folder/$i $carblog_path/$current_folder/$i $1 c
#		fi
#	done
#	
	#get files in Config
#	current_folder="Config"
#	files=$(ls -a $update_path/$current_folder 2>/dev/null)
#	for i in files; do
#		if [ -f "$update_path/$current_folder/$i" ] && [ "$i" != ".svn" ]; then
#			copy_file missing $update_path/$current_folder/$i $carblog_path/$current_folder/$i $i c
#		fi
#	done
#fi

#################  Check for log configuration, update if necessary ############

log_change=0
log_change=$(diff $update_path/sys_resources/logrotate.conf /etc/logrotate.conf | wc -l)

#overwrite logrotate.conf if any differences noted.
if [ "$log_change" -gt 0 ]; then
	echo "$date_stamp :: writing logrotate.conf to /etc" >> $U_LOG
	cp -f $update_path/sys_resources/logrotate.conf /etc 2>/dev/null
fi

#overwrite rsyslog if any differences noted.
log_change=0
log_change=$(diff $update_path/sys_resources/rsyslog /etc/logrotate.d/rsyslog | wc -l)
if [ "$log_change" -gt 0 ]; then
	echo "$date_stamp :: writing rsyslog to /etc/logrotate.d" >> $U_LOG
	cp -f $update_path/sys_resources/rsyslog /etc/logrotate.d/rsyslog 2>/dev/null
fi

#Add Logrotate to chron.daily
if [ ! -e "/etc/cron.hourly/logrotate" ]; then
	cp /etc/cron.daily/logrotate /etc/cron.hourly/logrotate
fi

################  Check for .joerc files, crontab files  #######################
i=".joerc"

if [ ! -e "$home_path/$i" ]; then
	copy_file copy $update_path/sys_resources/$i $home_path/$i $i c
else
	file_diff=$(diff $update_path/sys_resources/$i $home_path/$i | wc -l)
	if [ "$file_diff" -gt 0 ]; then
		copy_file change $update_path/sys_resources/$i $home_path/$i $i c
	fi
fi

############### Check crontab configuration
#i="crontab.list"
#crontab -l > system_crontab
#file_diff=$(diff $update_path/sys_resources/$i system_crontab | wc -l)
#rm -f system_crontab
#if [ "$file_diff" -gt 0 ]; then
#	echo "$date_stamp :: updating crontab" >> $U_LOG
#	crontab $update_path/sys_resources/crontab.list
#fi

##################  Update root directory files  ###############################
#get files in root
files=$(ls -a $update_path 2>/dev/null)
for i in $files; do
	if [ "$i" != "." ] && [ "$i" != ".." ]; then
		exist_flag=0
		#echo $i ##DEBUG
		if [ ! -e "$carblog_path/$i" ] && [ "$i" != ".svn" ]; then
			copy_file missing $update_path/$i $carblog_path/$i $i c
			if [ "$i" = "interface.sh" ]; then
				restart_flag=1
			fi
			exist_flag=1
		fi
		if [ -f "$update_path/$i" ] && [ "$i" != ".svn" ] && [ "$exist_flag" -eq 0 ]; then
			
			file_diff=$(diff $update_path/$i $carblog_path/$i | wc -l)
			#echo $file_diff #DEBUG
			if [ "$file_diff" -gt 0 ]; then
				copy_file change $update_path/$i $carblog_path/$i $i c
				if [ "$i" = "interface.sh" ]; then
					restart_flag=1
				fi
				#indicate that interface.sh changed
				#if [ "$i" = "interface.sh" ]
				#then
				#	restart_all_flag=1
				#fi 
				#echo "changed $i" ####DEBUG 
			fi
		fi
	fi
done

################# Update sys_resources files ###################################
current_folder="sys_resources"
files=$(ls -a $update_path/$current_folder 2>/dev/null)
for i in $files; do
	if [ "$i" != "." ] && [ "$i" != ".." ]; then
		exist_flag=0
		#echo $i ##DEBUG
		if [ ! -e "$carblog_path/$current_folder/$i" ] && [ "$i" != ".svn" ] && [ "$i" != "dms.cfg" ]; then
			if [ -d "$update_path/$current_folder/$i" ]; then #check for directory
				copy_file copy $update_path/$current_folder/$i $carblog_path/$current_folder/$i $i r
			else
				copy_file missing $update_path/$current_folder/$i $carblog_path/$current_folder/$i $i c
				if [ "$i" = "ingestmanager.sh" ] || [ "$i" = "reporter.sh" ]
				then
					restart_flag=1
				fi
				exist_flag=1
			fi
		fi


		if [ -d "$update_path/$current_folder/$i" ] && [ $i != ".svn" ]; then
			files2=$(ls -a $update_path/$current_folder/$i 2>/dev/null)
			for k in $files2; do
				if [ "$k" != "." ] && [ "$k" != ".." ]; then
					exist_flag=0
					#echo $i ##DEBUG
					if [ ! -e "$carblog_path/$current_folder/$i/$k" ] && [ $k != ".svn" ]; then
						copy_file missing $update_path/$current_folder/$i/$k $carblog_path/$current_folder/$i/$k $k c
						exist_flag=1
					fi
		
					#do not overwrite .display, .disabled1, or .usr1 as they are configuration files
					if [ -f $update_path/$current_folder/$i/$k ] && [ $k != ".svn" ] && [ $exist_flag -eq 0 ]; then
						#check to see if update file is different than carblogger file
						file_diff=$(diff $update_path/$current_folder/$i/$k $carblog_path/$current_folder/$i/$k | wc -l)
						#echo $file_diff #DEBUG
						if [ "$file_diff" -gt 0 ]; then             
							copy_file copy $update_path/$current_folder/$i/$k $carblog_path/$current_folder/$i/$k $k c
						fi
						#echo "changed $i" ####DEBUG
					fi
				fi
			done				
			
		fi
		if [ -f "$update_path/$current_folder/$i" ] && [ "$i" != "dms.cfg" ] && [ "$i" != "pastactions.txt" ] && [ "$i" != ".svn" ] && [ "$exist_flag" -eq 0 ]; then
			if [ "$i" != "carbput.sh" ] && [ "$i" != ".tmpcom" ]; then	
				file_diff=$(diff $update_path/$current_folder/$i $carblog_path/$current_folder/$i | wc -l)
				#echo $file_diff #DEBUG
				if [ "$file_diff" -gt 0 ]; then             
					copy_file change $update_path/$current_folder/$i $carblog_path/$current_folder/$i $i c
					if [ "$i" = "ingestmanager.sh" ] || [ "$i" = "reporter.sh" ]; then
						restart_flag=1
					fi
					#echo "changed $i" ####DEBUG
				fi
			fi
		fi	
	fi
done

############### Update documentation files #####################################
#current_folder="Documentation"    
#files=$(ls -a $update_path/$current_folder 2>/dev/null)
#printf %s "$files" | while IFS= read -r line
#do
#   echo "$line"
#done
#
#for i in $files
#do
#
#	exist_flag=0
#	#echo $i ##DEBUG
#	if [ ! -e "$update_path/$current_folder/$i" ]
#	then
#		echo "copying $update_path/$current_folder/$i to $carblog_path/$current_folder/$i" ###DEBUG
#		cp -f $update_path/$current_folder/$i $carblog_path/$current_folder
#		chown aqdms $carblog_path/$current_folder/$i
#		chmod 755 $carblog_path/$current_folder/$i
#		exist_flag=1
#	fi
#
#	if [ -f "$update_path/$current_folder/$i" ] && [ "$exist_flag" -eq 0 ]
#	then
#		file_diff=$(diff $update_path/$current_folder/$i $carblog_path/$current_folder/$i | wc -l)
#		#echo $file_diff #DEBUG
#		if [ "$file_diff" -gt 0 ]
#		then             
#			echo "copying $update_path/$current_folder/$i to $carblog_path/$current_folder/$i" ###DEBUG
#			cp -f $update_path/$current_folder/$i $carblog_path/$current_folder
#			chown aqdms $carblog_path/$current_folder/$i
#			chmod 755 $carblog_path/$current_folder/$i
#	        #echo "changed $i" ####DEBUG
#       fi
#  fi
#done

############### Update template files #####################################
current_folder="templates"    
files=$(ls -a $update_path/$current_folder 2>/dev/null)
for i in $files; do
	if [ "$i" != "." ] && [ "$i" != ".." ]; then
		exist_flag=0
		#echo $i ##DEBUG
		if [ ! -e "$carblog_path/$current_folder/$i" ] && [ "$i" != ".svn" ]; then
			copy_file missing $update_path/$current_folder/$i $carblog_path/$current_folder/$i $i c
			exist_flag=1
		fi	

		if [ -f "$update_path/$current_folder/$i" ] && [ "$exist_flag" -eq 0 ]; then
			file_diff=$(diff $update_path/$current_folder/$i $carblog_path/$current_folder/$i | wc -l)
			#echo $file_diff #DEBUG
			if [ "$file_diff" -gt 0 ]; then             
				copy_file copy $update_path/$current_folder/$i $carblog_path/$current_folder/$i $i c
				#echo "changed $i" ####DEBUG
			fi
		fi
	fi
done	

##############  Update resources folder and driver files #######################
current_folder="resources"
folders=$(ls $update_path/$current_folder 2>/dev/null)

#get list of driver folders in update
for i in $folders; do
	#if i exists in carblogger install, then check for file upgrade
	if [ -e $carblog_path/$current_folder/$i ] && [ "$i" != ".svn" ]; then
		usr_count=0
		#get list of files in driver folder i
		files=$(ls -a $update_path/$current_folder/$i 2>/dev/null)
		#check diff against update and carblogger install, if different then copy
		for k in $files; do
			if [ "$k" != "." ] && [ "$k" != ".." ]; then
				exist_flag=0
				#echo $i ##DEBUG
				if [ ! -e "$carblog_path/$current_folder/$i/$k" ] && [ "$k" != ".svn" ]; then
					copy_file missing $update_path/$current_folder/$i/$k $carblog_path/$current_folder/$i/$k $k c
					usr_count=$(ls -a $update_path/resources/$i | grep .usr1 | wc -l)
					if [ "$usr_count" -gt 0 ]; then ##if usr file found, flag to restart logger
						restart_flag=1
					fi
					exist_flag=1
				fi
	
				#do not overwrite .display, .disabled1, or .usr1 as they are configuration files
				if [ -f $update_path/$current_folder/$i/$k ] && [ $k != ".usr1" ] && [ $k != ".display" ] && [ $k != ".disabled1" ] && [ $k != ".svn" ]; then
					if [ $exist_flag -eq 0 ] && [[ ! "$k" =~ .[0-9][0-9]_[0-9]_bam_config ]]; then
						#check to see if update file is different than carblogger file
						file_diff=$(diff $update_path/$current_folder/$i/$k $carblog_path/$current_folder/$i/$k | wc -l)
						#echo $file_diff #DEBUG
						if [ "$file_diff" -gt 0 ]; then             
							copy_file change $update_path/$current_folder/$i/$k $carblog_path/$current_folder/$i/$k $k c
							##Check for actively used driver, look for .usr1 file
							usr_count=$(ls -a $carblog_path/$current_folder/$i | grep .usr1 | wc -l)
							if [ "$usr_count" -gt 0 ]; then ##if usr file found, flag to restart logger
								restart_flag=1
							fi
						fi
							#echo "changed $i" ####DEBUG
					fi
				fi
			fi
		done
	else	 #if folder doesn't exist, copy all files into the resources directory
		copy_file missing $update_path/$current_folder/$i $carblog_path/$current_folder/$i $i r
	fi
done

#####################   Run special scripts ###################################
#
#     special upgrade scripts are stored in sys_resoureces/upgrade_scripts
#
###############################################################################

current_folder="sys_resources/upgrade_scripts"
folders=$(ls $carblog_path/$current_folder 2>/dev/null)

for i in $folders; do
	if [ "$i" != "." ] && [ "$i" != ".." ]; then
		echo "$date_stamp :: Running upgrade script $i" >> $U_LOG
		bash $carblog_path/$current_folder/$i &
		wait $!
	fi
done	

#####################   shutdown and restart CARBLogger if necessary ##########

#MODIFIED TO REBOOT EACH WEEK
restart_flag="1"

if [ "$restart_flag" -eq 1 ]; then
	echo "$date_stamp :: Upgrade required reboot" >> $U_LOG
	echo -e "*******************  END UPGRADE     ************************************\n\n" >> $U_LOG
	/sbin/reboot
fi
